<?php

class Bot_Model_BotDb
{
    protected $_table_class;
    protected $_period;

    public function __construct()
    {
        $this->_table_class='Bot_Model_DbTable_Bot';
        
        $this->_period=3600*24*7;
        
        $this->emptyTrash();
    }
    
    /**
     * Returns an array with row object
     *
     * @return array
     */
    public function fetch()
    {
        $rows=array();
        
        $bot_tbl=new $this->_table_class();
        
        
        $_rows=$bot_tbl->fetchAll(null,array('tstamp ASC'));
    
        foreach($_rows as $row)
        {
            $rows[]=$row;
        }

        return $rows;
    }
    
    /**
     * Returns an array with row object by given resource
     *
     * @param string $resource
     * 
     * @return array
     */
    public function fetchByResource($resource)
    {
        $rows=array();
        
        $bot_tbl=new $this->_table_class();
        
        
        $_rows=$bot_tbl->fetchAll(array(
                                        'resource=?'=>$resource,
                                        'parsed=?'=>'0'
                                        ),
                                 array('id ASC')
                                );

        foreach($_rows as $row)
        {
            $rows[]=$row;
        }
        
        return $rows;
    }
    
    /**
     * Returns an array with row object by given uri
     *
     * @param string $uri
     * 
     * @return array
     */
    public function fetchByUri($uri)
    {
        $rows=array();
        
        $bot_tbl=new $this->_table_class();
        
        
        $_rows=$bot_tbl->fetchAll(array(
                                        'uri=?'=>$uri
                                        ),
                                 array('id ASC')
                                );

        foreach($_rows as $row)
        {
            $rows[]=$row;
        }
        
        return $rows;
    }
    
    /**
     * Returns an array with row object by given uri and resource
     *
     * @param string $uri
     * @param string $resource
     * 
     * @return array
     */
    public function fetchByUriAndResource($uri,$resource)
    {
        $rows=array();
        
        $bot_tbl=new $this->_table_class();
        
        
        $_rows=$bot_tbl->fetchAll(array(
                                        'uri=?'=>$uri,
                                        'resource=?'=>$resource
                                        ),
                                 array('id ASC')
                                );

        foreach($_rows as $row)
        {
            $rows[]=$row;
        }
        
        return $rows;
    }
    
    /**
     * Saves data to database
     *
     * @param mixed $row
     * 
     * @return int
     */
    public function save($row,$db=null)
    {
        if (is_array($row))
        {
            if (empty($db))
                $bot_tbl=new $this->_table_class();
            else
                $bot_tbl=new $this->_table_class(array('db'=>$db));
            
            if (!empty($row['id']))
            {
                return $bot_tbl->update($row,array('id=?'=>$row['id']));
            } else 
            {
                return $bot_tbl->insert($row);
            }
        }
    }
    
    /**
     * Deletes data from database
     *
     * @param mixed $row
     * @return int
     */
    public function delete($row)
    {
        if (is_array($row) && !empty($row['id']))
        {
            $bot_tbl=new $this->_table_class();

            return $bot_tbl->delete(array('id=?'=>$row['id']));
        }
    }
    
    /**
     * Clear table
     *
     * @return int
     */
    public function clear()
    {
        $bot_tbl=new $this->_table_class();

        return $bot_tbl->delete();
    }
    
    /**
     * Deletes trash from database
     *
     * @return int
     */
    public function emptyTrash()
    {
        $bot_tbl=new $this->_table_class();

        return $bot_tbl->delete(array('tstamp<?'=>time()-$this->_period));
    }
}

